package com.wframe.project.jnd.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wframe.project.jnd.rest.util.WebSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.wframe.project.jnd.dao.FloorDao;
import com.wframe.project.jnd.dao.InformationDao;
import com.wframe.project.jnd.dao.ProductDao;
import com.wframe.project.jnd.services.FloorService;

@Service
public class FloorServiceImpl implements FloorService{

	@Autowired
	FloorDao floorDao;
	@Autowired
	ProductDao productDao;
	@Autowired
	InformationDao informationDao;
	@Autowired
	protected RedisTemplate<String,String> redisTemplate ;

	@Override
	public List<Map> queryHomeFloor() {
		// TODO Auto-generated method stub
		List<JdataBean> list=floorDao.queryFloor(1);
		List<Map> floorList=new ArrayList<Map>();
		List<String> floorIds = new ArrayList<>();
		for(JdataBean floor : list) {
			floorIds.add(floor.getString("id"));
		}
		List<JdataBean> floorItems = floorDao.queryFloorItems(floorIds);
		for(JdataBean floor:list){
			List<JdataBean> itemlist = new ArrayList<>();
			for(JdataBean item : floorItems) {
				if (item.getString("floorid").equals(floor.getString("id"))){
					itemlist.add(item);
				}
			}
//			floor.put("itemlist", floorDao.queryFloorItem(floor.getString("id")));
			floor.put("itemlist", itemlist);
			List<JdataBean> productlist=productDao.queryFloorProduct(floor.getString("id"), null, 10, 1).getList();
			floor.put("productlist",productlist);
			JdataBean parms=new JdataBean();
			parms.put("code", floor.getString("channelcode"));
			List<Map> plist=informationDao.queryInfos(parms, 1, 1).getList();
			if(plist.size()>0){
				floor.put("floorphoto",plist.get(0).get("phototitle"));
				floor.put("floorinfoid",plist.get(0).get("id"));
				floor.put("floorinfotype",plist.get(0).get("type"));
				floor.put("floorinfourl",plist.get(0).get("url"));
			}
			floorList.add(floor.getDataMap());
		}
		return floorList;
	}
	public PageResult queryFloorProduct(String floorid,String itemid,int size,int pageno){
		return productDao.queryFloorProduct(floorid,itemid,size, pageno);
	}
	protected RedisSerializer<String> getRedisSerializer() { 
	    return redisTemplate.getStringSerializer(); 
	}
	public JdataBean getFloor(String id){
		return floorDao.getFloor(id);
	}
	public JdataBean getFloorItem(String id){
		return floorDao.getFloorItem(id);
	}

	@Override
	public List<Map> findHomeFloor(String userid) {
		List<JdataBean> list=floorDao.queryFloor(1);
		List<Map> floorList=new ArrayList<Map>();
		List<String> floorIds = new ArrayList<>();
		for(JdataBean floor : list) {
			floorIds.add(floor.getString("id"));
		}
		List<JdataBean> floorItems = floorDao.queryFloorItems(floorIds);
		for(JdataBean floor:list){
			List<JdataBean> itemlist = new ArrayList<>();
			for(JdataBean item : floorItems) {
				if (item.getString("floorid").equals(floor.getString("id"))){
					itemlist.add(item);
				}
			}
//			floor.put("itemlist", floorDao.queryFloorItem(floor.getString("id")));
			floor.put("itemlist",itemlist);
			List<JdataBean> productlist=productDao.findFloorProducts(userid, floor.getString("id"), null, 10, 1).getList();
			floor.put("productlist",productlist);
			JdataBean parms=new JdataBean();
			parms.put("code", floor.getString("channelcode"));
			List<Map> plist=informationDao.queryInfos(parms, 1, 1).getList();
			if(plist.size()>0){
				floor.put("floorphoto",plist.get(0).get("phototitle"));
				floor.put("floorinfoid",plist.get(0).get("id"));
				floor.put("floorinfotype",plist.get(0).get("type"));
				floor.put("floorinfourl",plist.get(0).get("url"));
			}
			floorList.add(floor.getDataMap());
		}
		return floorList;
	}
}
